#include <SGE.h>
#include "Bullet.h"
#include "Fighter.h"
#include "Enemy.h"


using namespace SGE;

const int kMaxBullets = 100;
const int kMaxEnemies = 100;
Bullet bullets[kMaxBullets];
Enemy enemies[kMaxEnemies];
Fighter fighter;

bool quit = false;
int bulletIndex = 0;
int enemyIndex = 0;

const int kMaxExplosions =100;
int explosionIndex =0;
SGE_Emitter explosions[kMaxExplosions];


// in names
 // m for members
// g for global
// s for statics
//k for constants
// p for pointers

float spawnDelay = 0.0f;

SGE_Sprite background;
SGE_Sound fireSound;
SGE_Sound explosionSound;

void UpdateGameObjects(float deltaTime)
{
	fighter.Update(deltaTime);
	for (int i = 0; i < kMaxBullets; i++)
	{
		bullets[i].Update(deltaTime);
	}
	for (int i = 0; i < kMaxEnemies; i++)
	{
		enemies[i].Update(deltaTime);
	}
	for (int i = 0; i < kMaxExplosions; i++)
	{
		explosions[i].Update(deltaTime);
	}
}

void FireBullets()
{
	

	if(Input_IsKeyPressed(Keys::SPACE))
	{
		fireSound.Play();
		if (!bullets[bulletIndex].IsActive())
		{
			SVector2 pos = fighter.Getposition();
			SVector2 vel(0.0f, -500.0f);
		
			bullets[bulletIndex].Fire(pos, vel);
			bulletIndex = (bulletIndex+1) % kMaxBullets;
		}
		if (!bullets[bulletIndex].IsActive())
		{
			SVector2 pos = fighter.Getposition();
			SVector2 vel(250.0f, -500.0f);
		
			bullets[bulletIndex].Fire(pos, vel);
			bulletIndex = (bulletIndex+1) % kMaxBullets;
		}
		if (!bullets[bulletIndex].IsActive())
		{
			SVector2 pos = fighter.Getposition();
			SVector2 vel(-250.0f, -500.0f);
		
			bullets[bulletIndex].Fire(pos, vel);
			bulletIndex = (bulletIndex+1) % kMaxBullets;
		}
	}
}

void SpawnEnemies(float deltaTime)
{
	spawnDelay -= deltaTime;
	if(spawnDelay <= 0.0f)
	{
		if(!enemies[enemyIndex].IsActive())
		{
			const int kWinWidth = IniFile_GetInt("WinWidth", 800);  // ini files
			const int kWinHeight = IniFile_GetInt("WinHeight", 600);
	SVector2 spawnpoint( kWinWidth * RandomFloat(0.1f, 1.0f ), -1.0f);
		
		enemies[enemyIndex].Spawn(spawnpoint);
		enemyIndex = (enemyIndex +1) % kMaxEnemies;
		}

		// reset delay time
		spawnDelay = RandomFloat (0.5f, 2.0f);
	}
}

void CheckCollision()
{
	for (int e = 0; e < kMaxEnemies; e++)
	{
		if(enemies[e].IsActive())
		{
			for (int b = 0; b < kMaxBullets; b++)
			{
				if(bullets[b].IsActive())
				{
					SCircle eCircle = enemies[e].GetBoundingCircle();
					SCircle bCircle = bullets[b].GetBoundingCircle();
					float xDiff = eCircle.center.x - bCircle.center.x;
					float yDiff = eCircle.center.y - bCircle.center.y;
					float dist = sqrt((xDiff * xDiff) + (yDiff * yDiff));
					float radii = eCircle.radius + bCircle.radius;		// chech intersect function
					if(dist <= radii)
					{
						enemies[e].Kill();
						bullets[b].Kill();
						explosions[explosionIndex].SetPosition(eCircle.center);
						explosions[explosionIndex].Start(100.0f,true);
						explosionIndex = (explosionIndex+1) % kMaxExplosions;
						explosionSound.Play();
						break;
					}
				}
			}
		}
	}
}

void SGE_Initialize()
{
	background.Load("space.jpg");
	const int kWinWidth = IniFile_GetInt("WinWidth", 800);  // ini files
	const int kWinHeight = IniFile_GetInt("WinHeight", 600);
	SVector2 center(kWinWidth * 0.5f, kWinHeight *0.7f);
	
	fighter.Load();
	fighter.SetPosition(center);
	fireSound.Load("photongun1.wav");
	explosionSound.Load("explosion.wav");
	for (int i = 0; i < kMaxBullets; i++)
	{
		bullets[i].Load();
	}
	for (int i = 0; i < kMaxEnemies; i++)
	{
		enemies[i].Load();
	}
	for (int i = 0; i < kMaxExplosions; i++)
	{
		explosions[i].Load("explosion.txt");
	}
}

void SGE_Terminate()
{
	background.Unload();
	fighter.Unload();

	fireSound.Unload();
	explosionSound.Unload();

	for (int i = 0; i < kMaxBullets; i++)
	{
		bullets[i].Unload();
	}
	for (int i = 0; i < kMaxEnemies; i++)
	{
		enemies[i].Unload();
	}
	for (int i = 0; i < kMaxExplosions; i++)
	{
		explosions[i].Unload();
	}
}

bool SGE_Update(float deltaTime)
{
	
	UpdateGameObjects(deltaTime);
	FireBullets();
	SpawnEnemies(deltaTime);
	CheckCollision();

	

	if(Input_IsKeyPressed(Keys::ESCAPE))
	{
		quit = true;
	}
	return quit;
}

void SGE_Render()
{
	background.Render();
	for (int i = 0; i < kMaxBullets; i++)			// Order of the render matters!
	{
		bullets[i].Render();
	}
	for (int i = 0; i < kMaxEnemies; i++)
	{
		enemies[i].Render();
	}
	
	fighter.Render();

	for (int i = 0; i < kMaxExplosions; i++)
	{
		explosions[i].Render(true);					// Remember to change the name of the function when you copy and paste!!
	}
}